﻿/**
 * Parse TreeGrid format strings as Dictionary
 */
import AcroMLDD from "../acroML.DD.mjs";

class AcroMLTreeGridDD extends AcroMLDD {
	constructor(){
		super();
		this.content = "";
	}
	// / <summary>
	// / 解析treegrid格式json文件，放到Hash表中
	// / </summary>
	buildingDD(){
		let data=JSON.parse(this.content);
		let self=this;
		function scanRows(rows){
			for(let i=0;i<rows.length;i++){
				let row=rows[i];
				let DDKey=row.DDKey;
				if (data.displayValueColumns){
					for(let j=0;j<data.displayValueColumns.length;j++){
						let column=data.displayValueColumns[j];
						let LCID=column.lng.LCID;
						if (column.SeqNos){
							for(let k=0;k<column.SeqNos.length;k++){
								let SeqNo=column.SeqNos[k];
								let DisplayValue=row[LCID+'-'+SeqNo];
								if (!DisplayValue) continue;
								self.addDisplayValue(DDKey,LCID,SeqNo,DisplayValue);
							}
						}
						else{
							let SeqNo='0';
							let DisplayValue=row[LCID+'-'+SeqNo];
							if (!DisplayValue) continue;
							self.addDisplayValue(DDKey,LCID,SeqNo,DisplayValue);
						}
					}
				}
				else{
					for(let field in row){
						if (field=='DDKey' || field=='SortNumber') continue;
						let parts=field.split('-');
						let LCID=parts[0];
						if (isNaN(parseFloat(LCID))|| Number.isFinite(LCID)) continue;
						LCID=parseInt(LCID);
						let SeqNo='0';
						if (parts.length>1)	SeqNo=parts[1];
						let DisplayValue=row[field];
						if (!DisplayValue) continue;
						self.addDisplayValue(DDKey,LCID,SeqNo,DisplayValue);
					}
				}
				if (row.children) scanRows(row.children);
			}
		}
		scanRows(data.rows);
		super.buildingDD();		
	}
}
export default AcroMLTreeGridDD;
AcroMLDD.registerDD('.treegrid.json','utf8',[],AcroMLTreeGridDD);
export {AcroMLTreeGridDD as TreeGridDD};